package service;

import dao.JPADAOFactory;
import dao.exception.AlreadyEntityExistsException;
import dao.exception.NotEntityFoundException;
import entity.Acompanante;
import javax.persistence.EntityManager;
import service.exception.DuplicateObjectException;
import service.exception.ObjectNotFoundException;
import valueObject.AcompananteVO;

/**
 *
 * @author Juan Sebastian Hernández Serrato
 */
public class AcompananteService extends AbstractService<AcompananteVO, Acompanante> {

    @Override
    public int create(AcompananteVO valueObject, EntityManager em) throws DuplicateObjectException {
        Acompanante acompanante = valueObject.toEntity();
        try {
            JPADAOFactory.getInstance().getAcompananteDAO().create(acompanante, em);
            return acompanante.getId();
        } catch (AlreadyEntityExistsException ex) {
            throw new DuplicateObjectException();
        }
    }

    @Override
    public AcompananteVO findById(int id, EntityManager em) throws ObjectNotFoundException {
        AcompananteVO acompananteVO;
        try {
            acompananteVO = JPADAOFactory.getInstance().getAcompananteDAO().findById(id, em).toValueObject();
        } catch (NotEntityFoundException ex) {
            throw new ObjectNotFoundException();
        }
        return acompananteVO;
    }

    @Override
    public void update(AcompananteVO valueObject, EntityManager em) throws ObjectNotFoundException {
        try {
            Acompanante acompanante = JPADAOFactory.getInstance().getAcompananteDAO().findById(valueObject.getId(), em);
            acompanante = valueObject.toEntity();
            JPADAOFactory.getInstance().getAcompananteDAO().update(acompanante, em);
        } catch (NotEntityFoundException ex) {
            throw new ObjectNotFoundException();
        }
    }

    @Override
    public void delete(int id, EntityManager em) throws ObjectNotFoundException {
        try {
            Acompanante acompanante = JPADAOFactory.getInstance().getAcompananteDAO().findById(id, em);
            JPADAOFactory.getInstance().getAcompananteDAO().delete(acompanante, em);
        } catch (NotEntityFoundException ex) {
             throw new ObjectNotFoundException();
        }
    }
}
